package org.example.day06;

/**
 * 第二章节:2.3.1.4：使用volatile关键字解决2.3.3中出现的死循环
 * volatile作用:
 *      ①如果未使用，则像2.3.2修改了主内存，而线程私有内存中的值达不到同步，而出现死循环
 *      ②如果使用，则《强制使得线程私有内存和主内存达到同步》,从而解决死循环.
 */
public class MyThread4 extends Thread{
    volatile private boolean isRunning = true;

    public boolean isRunning() {
        return isRunning;
    }

    public void setRunning(boolean running) {
        isRunning = running;
    }

    @Override
    public void run() {
        System.out.println("进入了run");
        while(isRunning == true){

        }
        System.out.println("线程被停止了!");
    }

    public static void main(String[] args) {
        try {
            MyThread4 m4 = new MyThread4();
            m4.start();
            m4.sleep(1000);
            m4.setRunning(false);
            System.out.println("已经赋值为false");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
